/**
 * 商品明细
 * Created by wenchuan.li on 2018/06/14 23:43.
 */
define(function () {

    var self,
        app = win.app,
        others = {
            countAmount: 0,
            GWBHACTI: 'N',
            from:''
        },
        pageParameters,
        ordersOld = []
    ;

    function setCountAmount() {
        others.GWBHACTI = pageParameters.GWBHACTI;

        var c = 0;
        pageParameters.orders.forEach(function (v) {
            c += ((isNaN(Number(v.GWAP100)) ? 0 : Number(v.GWAP100)) * Number(v.GWCM003));
        });
        ordersOld.forEach(function (v) {
            c += ((isNaN(Number(v.GWBK004)) ? 0 : Number(v.GWBK004)) * Number(v.GWBK003));
        });
        others.countAmount = c;
        pageParameters.updateAmount(c);
    }

    function showNoData() {
        if (pageParameters.orders.length == 0 && ordersOld.length == 0) {
            app.showNoData($('#scroll1'), '无商品明细...');
        } else {
            app.hideNoData($('#scroll1'));
        }
    }

    var Spmx = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _readyBefore: function () {
                    pageParameters = self._pageParameters;
                    ordersOld = pageParameters.ordersOld;
                    others.from = pageParameters._from;
                }
            }, win);
        },
        getData: function () {
            app.ajax({
                xurl: "api/wx/gwat161/gwat161Spmx",
                xdata: {
                    GWBKNAME: pageParameters.GWBHNAME,
                    GWBHSEQ: pageParameters.GWBHSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        ordersOld.pushArray(result.data.data);
                    }
                }
            });
        },
        _lazy: function () {
            this._super();

            //if (pageParameters.GWBHNAME && ordersOld.length == 0) {
            //    self.getData();
            //}

            new Vue({
                el: app.getByid(doc, 'scroll1'),
                data: {
                    items: pageParameters.orders,
                    itemsOld: ordersOld,
                    GWBHACTI: pageParameters.GWBHACTI
                },
                methods: {
                    showmsg: function () {
                        app.toast('该拜访记录已经审核，无法修改');
                        return;
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'contentFooter'),
                data: others,
                methods: {
                    okOrder: function () {
                        app.back(1);
                    },
                    addOrder: function () {
                        app.openPage({
                            src: "hn-bfjltb/goods-classes.html",
                            args: {
                                _targer: 'add',
                                cb: function (order) {
                                    pageParameters.orders.push(order);
                                    app.back(1);
                                    showNoData();
                                }
                            }
                        });
                    }
                }
            });

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });

            showNoData();
            setCountAmount();

        },
        _ready: function () {
            Vue.component('gwc-listitem', {
                template: app.getByid(doc, 'cartitem-template'),
                props: {
                    model: Array
                },
                watch: {
                    'model': {
                        handler: function (val, oldVal) {
                            setCountAmount();
                        },
                        deep: true
                    }
                },
                methods: {
                    minus: function (val) {
                        if (val.GWCM003 > 1) {
                            val.GWCM003--;
                        }
                    },
                    plus: function (val) {
                        val.GWCM003++;
                    },
                    showDetail: function (val) {
                        app.openPage({
                            src: "hn-bfjltb/goods-info.html",
                            args: {
                                _targer: 'read',
                                item: app.cloneJSON(val),
                                cb: function (updateData) {
                                    app.extend(val, updateData);
                                    val.GWAP100 = val.GWAP100_1;
                                    app.back(1);
                                }
                            }
                        });
                    },
                    deleteDetail: function (val, elobj) {
                        pageParameters.orders.remove(val);
                        mui.swipeoutClose($(elobj.target).parents("li").get(0));
                        showNoData();
                    }
                }
            });
            Vue.component('gwcold-listitem', {
                template: app.getByid(doc, 'cartitemold-template'),
                props: {
                    model: Array
                },
                watch: {
                    'model': {
                        handler: function (val, oldVal) {
                            setCountAmount();
                        },
                        deep: true
                    }
                },
                methods: {
                    minus: function (val) {
                        if (val.GWBK003 > 1) {
                            val.GWBK003--;
                        }
                    },
                    plus: function (val) {
                        val.GWBK003++;
                    },
                    showDetail: function (val) {
                        app.openPage({
                            src: "hn-bfjltb/goods-info.html",
                            args: {
                                _targer: 'read',
                                item: {
                                    GWAM001: val.GWBK001,//料号
                                    GWCM003: val.GWBK003,//数量
                                    GWAP100: 0,//单价
                                    GWAP100_1: val.GWBK004,//单价
                                    GWBK008: val.GWBK008,//预计购买日期
                                    GWBK009: val.GWBK009,//预计回款日期
                                    GWAM015: val.GWAM015,//商品图片
                                    GWAMl003: val.GWAMl003//料号简称
                                },
                                cb: function (updateData) {
                                    //app.extend(val, updateData);
                                    app.back(1);
                                    val.GWBK001 = updateData.GWAM001;
                                    val.GWBK003 = updateData.GWCM003;
                                    val.GWBK004 = updateData.GWAP100_1;
                                    val.GWBK008 = updateData.GWBK008;
                                    val.GWBK009 = updateData.GWBK009;
                                    val.GWAM015 = updateData.GWAM015;
                                    val.GWAMl003 = updateData.GWAMl003;
                                }
                            }
                        });
                    },
                    deleteDetail: function (val, elobj) {
                        function del() {
                            ordersOld.remove(val);
                            mui.swipeoutClose($(elobj.target).parents("li").get(0));
                            showNoData();
                        }

                        if (pageParameters._from == 'add') {
                            // 由于商品明细没次拜访记录都是独立的，所有不能进行ajax删除操作
                            del();
                        } else if (pageParameters._from == 'edit') {
                            app.ajax({
                                xurl: "api/wx/gwat161/delGwbkByKey",
                                xdata: {
                                    GWBKNAME: pageParameters.GWBHNAME,
                                    GWBKID: pageParameters.GWAK001,
                                    GWBKSEQ: val.GWBKSEQ,
                                    GWBKSEQ2: val.GWBKSEQ2
                                },
                                loadding: true,
                                confirm: {
                                    msg: '确定删除？'
                                },
                                cb: function (result) {
                                    if (result.ok) {
                                        del();
                                    }
                                }
                            });
                        }
                    }
                }
            });
        }
    });

    return new Spmx();

});